<template>
  <div>
    <div>
      <div>
        <el-input
          style="width: 200px; margin-right: 20px"
          size="mini"
          placeholder="定位名称"
          v-model="searchPosition.name"
          clearable
          @keydown.enter.native="
            searchPosition.currentPage = 1
            searchPosition.size = 10
            initPosition()
          "
        ></el-input>
        <el-select
          v-model="searchPosition.parentId"
          size="mini"
          style="width: 200px; margin-right: 20px"
          placeholder="父级定位"
          clearable
          filterable
          @change="
            searchPosition.currentPage = 1
            searchPosition.size = 10
            initPosition()
          "
        >
          <el-option v-for="item in parentPosition" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-button
          icon="el-icon-delete"
          type="warning"
          size="mini"
          @click="
            searchPosition.name = ''
            searchPosition.parentId = null
          "
        >
          重置
        </el-button>
        <el-button
          type="success"
          size="mini"
          icon="el-icon-search"
          @click="
            searchPosition.currentPage = 1
            searchPosition.size = 10
            initPosition()
          "
        >
          搜索
        </el-button>
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-plus"
          @click="showAddPositionView"
          v-if="
            roleButton.buttonLevelComponents !== null &&
            roleButton.buttonLevelComponents.indexOf('ArknightsPosition') !== -1
          "
        >
          添加定位
        </el-button>
      </div>
    </div>
    <div style="margin-top: 10px">
      <el-table
        size="mini"
        :data="positionData"
        border
        stripe
        :row-class-name="tableRowClassName"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column prop="name" align="center" width="120" label="定位名称"></el-table-column>
        <el-table-column prop="parent.name" align="center" width="120" label="父级定位"></el-table-column>
        <el-table-column label="定位特性">
          <template slot-scope="scope">
            <el-tooltip
              v-if="scope.row.characteristic && toText(scope.row.characteristic).length > 50"
              class="item"
              effect="dark"
              placement="bottom"
            >
              <span v-html="scope.row.characteristic.replace(/\n/gm, '<br>')" slot="content">
                {{ scope.row.characteristic }}
              </span>
              <span
                v-if="scope.row.characteristic"
                v-html="$options.filters.ellipsisHtml(scope.row.characteristic, 50)"
              ></span>
            </el-tooltip>
            <span v-else-if="scope.row.characteristic" v-html="toText(scope.row.characteristic)" slot="content"></span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="100">
          <template slot-scope="scope">
            <el-button
              v-if="
                roleButton.buttonLevelComponents !== null &&
                roleButton.buttonLevelComponents.indexOf('ArknightsPosition') !== -1
              "
              size="mini"
              style="padding: 3px"
              type="primary"
              @click="showUpdatePositionView(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-if="
                roleButton.buttonLevelComponents !== null &&
                roleButton.buttonLevelComponents.indexOf('ArknightsPosition') !== -1
              "
              size="mini"
              style="padding: 3px"
              type="danger"
              @click="deletePosition(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div style="display: flex; justify-content: flex-end; margin-top: 5px">
      <el-pagination
        background
        layout="sizes, prev, pager, next, jumper, ->, total, slot"
        :total="total"
        :current-page="searchPosition.currentPage"
        :page-size="searchPosition.size"
        @current-change="currentChange"
        @size-change="sizeChange"
      ></el-pagination>
    </div>

    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      @close="closeDialog()"
      :visible.sync="dialogVisible"
      width="800px"
    >
      <el-form ref="positionForm" :model="position" :rules="rules">
        <table>
          <tr>
            <el-form-item label="定位名称" prop="name">
              <el-input
                size="mini"
                style="width: 200px"
                v-model="position.name"
                placeholder="请输入定位名称"
              ></el-input>
            </el-form-item>
          </tr>
          <tr>
            <el-form-item label="父级定位" prop="parentId">
              <el-select
                v-model="position.parentId"
                size="mini"
                style="width: 200px; margin-right: 20px"
                placeholder="请选择父级定位"
                clearable
                filterable
              >
                <el-option
                  v-for="item in parentPosition"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </tr>
          <tr>
            <el-form-item label="定位特性">
              <div id="richText"></div>
            </el-form-item>
          </tr>
        </table>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doPosition">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// wangeditor富文本
import E from 'wangeditor'
import store from '@/store'
import {
  addArknightsPosition,
  delArknightsPosition,
  arknightsParentPositions,
  arknightsPositionPageData,
  upArknightsPosition
} from '@/api/arknights/position'
let editor

export default {
  name: 'ArknightsPosition',
  data() {
    return {
      positionData: [],
      parentPosition: [],
      loading: false,
      total: 0,
      currentPage: 1,
      size: 10,
      searchPosition: {
        name: '',
        parentId: null,
        currentPage: 1,
        size: 10
      },
      position: {
        id: null,
        name: '',
        parentId: null,
        characteristic: ''
      },
      dialogVisible: false,
      title: '',
      rules: {
        name: [{ required: true, message: '请输入定位名称', trigger: 'blur' }],
        parentId: [{ required: true, message: '请选择父级定位', trigger: 'change' }]
      },
      roleButton: store.getters.currentRole
    }
  },
  computed: {},
  created() {
    // 富文本 解决页面切换失效问题
    editor = ''
  },
  mounted() {
    this.initPosition()
    this.initParentPosition()
  },
  methods: {
    initPosition() {
      this.loading = true
      arknightsPositionPageData(this.searchPosition).then(resp => {
        this.loading = false
        if (resp) {
          this.total = resp.total
          this.positionData = resp.data
        }
      })
    },
    initParentPosition() {
      arknightsParentPositions().then(resp => {
        if (resp) {
          this.parentPosition = resp
        }
      })
    },
    currentChange(page) {
      this.searchPosition.currentPage = page
      this.initPosition()
    },
    sizeChange(page) {
      this.searchPosition.size = page
      this.initPosition()
    },
    deletePosition(data) {
      this.$confirm('此操作将永久删除[' + data.name + ']定位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delArknightsPosition(data.id).then(resp => {
            if (resp) {
              this.initPosition()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    showAddPositionView(data) {
      this.position = {
        id: null,
        name: '',
        parentId: null,
        characteristic: ''
      }
      this.title = '添加定位'
      this.dialogVisible = true
      // 渲染富文本
      this.$nextTick(() => {
        if (!editor) {
          editor = new E('#richText')
          editor.config.height = 300
          // 配置上传图片地址
          editor.config.uploadImgServer = process.env.VUE_APP_UPLOAD_API_HOST + '/files/rich/upload'
          // 自定义fileName
          editor.config.uploadFileName = 'file'
          editor.create()
        }
        // 清空内容
        editor.txt.html('')
      })
    },
    showUpdatePositionView(data) {
      // 拷贝
      this.position = Object.assign({}, data)
      this.title = '编辑定位'
      this.dialogVisible = true
      // 渲染富文本
      this.$nextTick(() => {
        if (!editor) {
          editor = new E('#richText')
          editor.config.height = 300
          // 配置上传图片地址
          editor.config.uploadImgServer = process.env.VUE_APP_UPLOAD_API_HOST + '/files/rich/upload'
          // 自定义fileName
          editor.config.uploadFileName = 'file'
          editor.create()
        }
        // 内容赋值
        editor.txt.html('')
        editor.txt.html(this.position.characteristic)
      })
    },
    closeDialog() {
      this.$refs['positionForm'].resetFields()
    },
    doPosition() {
      if (this.position.id) {
        // 编辑
        this.$refs['positionForm'].validate(valid => {
          if (valid) {
            // 富文本需要手动赋值
            this.position.characteristic = editor.txt.html()
            upArknightsPosition(this.position).then(resp => {
              if (resp) {
                this.dialogVisible = false
                this.initPosition()
                this.initParentPosition()
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      } else {
        // 添加
        this.$refs['positionForm'].validate(valid => {
          if (valid) {
            // 富文本需要手动赋值
            this.position.characteristic = editor.txt.html()
            addArknightsPosition(this.position).then(resp => {
              if (resp) {
                this.dialogVisible = false
                this.initPosition()
                this.initParentPosition()
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
}
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

label {
  display: inline-block;
  width: 100px;
  text-align: right;
}
</style>
